package com.bezdelnick.forum.dao.hibernate;

import com.bezdelnick.forum.dao.IQuestionDao;
import com.bezdelnick.forum.dao.ITypeDao;
import com.bezdelnick.forum.domain.Question;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.07.12
 * Time: 12:43
 * To change this template use File | Settings | File Templates.
 */
public class QuestionDaoImpl implements IQuestionDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(ITypeDao.class);

    @Autowired(required = true)
    private SessionFactory sessionFactory;

    @Override
    public List<Question> loadQuestions() {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return session.createCriteria(Question.class).list();
        } catch (Error error) {
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public Question getQuestionById(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            Question question = (Question) session.get(Question.class, id); //session.load(Question.class, id);
            return question;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Question> loadQuestionsByTypeId(long id) {

        /*
        WARNING!!! - method is not valid
         */

        Session session = null;
        try {
            session = sessionFactory.openSession();
            return session.createCriteria(Question.class).add(Property.forName("typeId").eq(id)).list();
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Question> loadQuestionsBySubtypeId(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            return session.createCriteria(Question.class).add(Property.forName("subtypeId").eq(id)).list();
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return null;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean insertQuestion(Question question) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.save(question);
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean updateQuestion(Question question) {
        Session session = null;
        try {
            session = sessionFactory.openSession();

            String hql =
                    "update Question question " +
                    "set " +
                        "title= :title, " +
                        "textQuestion= :question, " +
                        "typeId= :type, " +
                        "subtypeId= :subtype " +
                    "where id= :id";

            Query query = session.createQuery(hql);
            query.setString("title", question.getTitle());
            query.setString("question", question.getTextQuestion());
            query.setLong("type", question.getTypeId());
            query.setLong("subtype", question.getSubtypeId());
            query.setLong("id", question.getId());
            query.executeUpdate();

            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    @Override
    public boolean deleteQuestion(long id) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
//            Question question = new Question();
//            question.setId(id);
//            session.delete(question);

            String hql = "delete from Question answer where id= :id";
            session.createQuery(hql).setLong("id", id).executeUpdate();
            return true;
        } catch (Error error) {
            LOGGER.error(error.getMessage());
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }
}
